<?php

/**
 * Helpers Class for whole CMS
 * 
 * 
 * @author Tuan Nguyen <nganhtuan63@gmail.com>
 * @version 1.0
 * @package backend.components
 */
class LGHelpers {

    public static function encode($text, $rplChar = '', $upWords = true) {
        $encodedText = null;
        if ($upWords) {
            $encodedText = ucwords($text);
        } else {
            $encodedText = strtolower($text);
        }

        if ($rplChar == '') {
            $encodedText = preg_replace('/\s[\s]+/', '', $encodedText);    // Strip off multiple spaces
            $encodedText = preg_replace('/[\s\W]+/', '', $encodedText);    // Strip off spaces and non-alpha-numeric
        } else {
            $encodedText = preg_replace('/\s[\s]+/', $rplChar, $encodedText);    // Strip off multiple spaces
            $encodedText = preg_replace('/[\s\W]+/', $rplChar, $encodedText);    // Strip off spaces and non-alpha-numeric
            $encodedText = preg_replace('/^[\\' . $rplChar . ']+/', '', $encodedText); // Strip off the starting $rplChar
            $encodedText = preg_replace('/[\\' . $rplChar . ']+$/', '', $encodedText); // // Strip off the ending $rplChar
        }
        return $encodedText;
    }

    public static function getQuoteText($text, $lenght) {
        //$text = preg_replace('/<script>/', '', $text);
        //$text = preg_replace('/<\/script>/', '', $text);
        //$text = preg_replace('/<script/', '', $text);
        $textArray = explode(' ', $text);
        $result = "";
        $count = count($textArray);
        if ($count > $lenght) {
            for ($i = 0; $i < $lenght; $i++) {
                $result = $result . $textArray[$i] . " ";
            }
            return $result . "...";
        }else
            return $text;
    }

    public static function getStatusMenu() {
        $arr = array();
        $arr[] = array('id' => LGConstant::PUBLISH_STATUS, 'name' => t('PUBLISH_STATUS'));
        $arr[] = array('id' => LGConstant::UNPUBLISH_STATUS, 'name' => t('UNPUBLISH_STATUS'));
        return $arr;
    }

    public static function getFeaturedArticle() {
        $arr = array();
        $arr[] = array('id' => LGConstant::NO_FEATURED, 'name' => t('NO_FEATURED'));
        $arr[] = array('id' => LGConstant::YES_FEATURED, 'name' => t('YES_FEATURED'));
        return $arr;
    }
    public static function BUIgnoreRows(){
        return array( "billable", "est_research_hours", "est_dev_hours", "est_qahours", "est_starton", "est_ready_for_service");
    }
    
    public static function getRequiredFields(){
        return array( 'environment','status','priority','team','title','summary','ranking', 'billable');
    }
     public static function getRequiredIssueFields(){
        return array('project','type','summary','priority','severity','status','resolution','platform');
    }

    public static function getCommentOption() {
        $arr = array();
        $arr[] = array('id' => LGConstant::CAN_NOT_COMMENT, 'name' => t('CAN_NOT_COMMENT'));
	$arr[] = array('id' => LGConstant::CAN_COMMENT, 'name' => t('CAN_COMMENT'));
        return $arr;
    }

    public static function getCommentStatuses() {
        $arr = array();
        $arr[] = array('id' => LGConstant::COMMENT_STATUS_NEW, 'name' => t('COMMENT_STATUS_NEW'));
        $arr[] = array('id' => LGConstant::COMMENT_STATUS_APPROVED, 'name' => t('COMMENT_STATUS_APPROVED'));
        $arr[] = array('id' => LGConstant::COMMENT_STATUS_REJECTED, 'name' => t('COMMENT_STATUS_REJECTED'));
        return $arr;
    }
    
    public static function getNameCommentStatus($idStatus) {
        $name = t('COMMENT_STATUS_NEW');
        if ($idStatus == LGConstant::COMMENT_STATUS_APPROVED)
            $name = t('COMMENT_STATUS_APPROVED');
        else if ($idStatus == LGConstant::COMMENT_STATUS_REJECTED)
            $name = t('COMMENT_STATUS_REJECTED');
        return $name;
    }

    public static function getRobots() {
        $arr = array();
        $arr[] = array('id' => LGConstant::INDEX_FOLLOW, 'name' => t('INDEX_FOLLOW'));
        $arr[] = array('id' => LGConstant::NOINDEX_FOLLOW, 'name' => t('NOINDEX_FOLLOW'));
        $arr[] = array('id' => LGConstant::INDEX_NOFOLLOW, 'name' => t('INDEX_NOFOLLOW'));
        $arr[] = array('id' => LGConstant::NOINDEX_NOFOLLOW, 'name' => t('NOINDEX_NOFOLLOW'));
        return $arr;
    }

    public static function getAccess() {
        $arr = array();
        $arr[] = array('id' => LGConstant::ACCESS_PUBLIC, 'name' => t('ACCESS_PUBLIC'));
        $arr[] = array('id' => LGConstant::ACESS_REGISTERED, 'name' => t('ACCESS_REGISTERED'));
        return $arr;
    }

    public static function getTargetWindow() {
        $arr = array();
        $arr[] = array('id' => LGConstant::TARGET_WIN_PARENT, 'name' => t('TARGET_WIN_PARENT', 'menu'));
        $arr[] = array('id' => LGConstant::TARGET_WIN_WITH_NAVIGATION, 'name' => t('TARGET_WIN_WITH_NAVIGATION', 'menu'));
        $arr[] = array('id' => LGConstant::TARGET_WIN_WITHOUT_NAVIGATION, 'name' => t('TARGET_WIN_WITHOUT_NAVIGATION', 'menu'));
        return $arr;
    }

    public static function getDefaultPages() {
        $arr = array();
        $arr[] = array('id' => LGConstant::DEFAULT_PAGE_YES, 'name' => t('YES'));
        $arr[] = array('id' => LGConstant::DEFAULT_PAGE_NO, 'name' => t('NO'));
        return $arr;
    }

     public static function getActiveStatusListData() {
        $arr = array();
        $arr[] = array('id' => LGConstant::ACTIVATION_STATUS_YES, 'name' => t('YES'));
        $arr[] = array('id' => LGConstant::ACTIVATION_STATUS_NO, 'name' => t('NO'));
        return $arr;
    }
    
    public static function getValueSettings($name) {
        $setting = Settings::model()->findAll();
        return $setting[0]->$name;
    }

    public static function getStatusCategory() {
        $arr = array();
        $arr[] = array('id' => LGConstant::PUBLISH_STATUS, 'name' => t('PUBLISH_STATUS'));
        $arr[] = array('id' => LGConstant::UNPUBLISH_STATUS, 'name' => t('UNPUBLISH_STATUS'));
        return $arr;
    }

    public static function getStatusAlbum() {
        $arr = array();
        $arr[] = array('id' => LGConstant::PUBLISH_STATUS, 'name' => t('PUBLISH_STATUS'));
        $arr[] = array('id' => LGConstant::UNPUBLISH_STATUS, 'name' => t('UNPUBLISH_STATUS'));
        return $arr;
    }

    public static function getStatusArticle() {
        $arr = array();
        $arr[] = array('id' => LGConstant::PUBLISH_STATUS, 'name' => t('PUBLISH_STATUS'));
        $arr[] = array('id' => LGConstant::UNPUBLISH_STATUS, 'name' => t('UNPUBLISH_STATUS'));
        $arr[] = array('id' => LGConstant::TRASHED_STATUS, 'name' => t('TRASHED_STATUS'));
        return $arr;
    }

    public static function getStatusSendEmail() {
        $arr = array();
        $arr[] = array('id' => LGConstant::EMAIL_SENT, 'name' => t('EMAIL_SENT'));
        $arr[] = array('id' => LGConstant::EMAIL_NOT_SENT, 'name' => t('EMAIL_NOT_SENT'));
        return $arr;
    }

    public static function getStatusDownloaded() {
        $arr = array();
        $arr[] = array('id' => LGConstant::DOWNLOADED, 'name' => t('DOWNLOADED'));
        $arr[] = array('id' => LGConstant::NOT_DOWNLOADED, 'name' => t('NOT_DOWNLOADED'));
        return $arr;
    }
    
    public static function getDataType() {
        $arr = array();
        $arr[] = array('id' => DataType::INTEGER, 'name' => DataTypeString::INTEGER);
        $arr[] = array('id' => DataType::FLOAT, 'name' => DataTypeString::FLOAT);
        $arr[] = array('id' => DataType::STRING, 'name' => DataTypeString::STRING);
        $arr[] = array('id' => DataType::DATETIME, 'name' => DataTypeString::DATETIME);
        $arr[] = array('id' => DataType::BOOLEAN, 'name' => DataTypeString::BOOLEAN);
        return $arr;
    }

    public static function getControlType() {
        $arr = array();
        $arr[] = array('id' => ControlType::TEXTBOX, 'name' => ControlTypeString::TEXTBOX);
        $arr[] = array('id' => ControlType::CHECKBOX, 'name' => ControlTypeString::CHECKBOX);
        $arr[] = array('id' => ControlType::RADIOBUTTON, 'name' => ControlTypeString::RADIOBUTTON);
        $arr[] = array('id' => ControlType::TEXTAREA, 'name' => ControlTypeString::TEXTAREA);
        $arr[] = array('id' => ControlType::COMBOBOX, 'name' => ControlTypeString::COMBOBOX);
        $arr[] = array('id' => ControlType::DATE_PICKER, 'name' => ControlTypeString::DATE_PICKER);
        $arr[] = array('id' => ControlType::EDITOR_BASIC, 'name' => ControlTypeString::EDITOR_BASIC);
        $arr[] = array('id' => ControlType::EDITOR_ADVANCED, 'name' => ControlTypeString::EDITOR_ADVANCED);
        $arr[] = array('id' => ControlType::SELECT_USER, 'name' => ControlTypeString::SELECT_USER);
        return $arr;
    }
     public static function getFieldVisibility() {
        $arr = array();
        $arr[] = array('id' => FieldVisibility::VISIBLE, 'name' => t('YES'));
        $arr[] = array('id' => FieldVisibility::INVISIBLE, 'name' => t('NO'));
        return $arr;
    }
    public static function getCustomField() {
        $arr = array();
        $arr[] = array('id' => CustomField::YES, 'name' => t('YES'));
        $arr[] = array('id' => CustomField::NO, 'name' => t('NO'));
        return $arr;
    }

    public static function getLanguage($languageCode) {
        if ($languageCode == "*") {
            $language = "All";
        } else {
            $languageModel = Language::model()->find('code = :code', array(':code' => $languageCode));
            if ($languageModel != null) {
		$language = $languageModel->value;
	    } else {
		$language = "";
	    }
        }
	
        return $language;
    }

    public static function getPageSize($records = 0) {
        if (isset($_GET['pageSize'])) {
            Yii::app()->user->setState(app()->controller->id . 'pageSize', (int) $_GET['pageSize']);
            unset($_GET['pageSize']);
        }
        $pageSize = Yii::app()->user->getState(app()->controller->id . 'pageSize', LGHelpers::getValueSettings('list_limit'));
        if ($pageSize == 0)
            $pageSize = $records;
        //var_dump($records);
        return $pageSize;
    }

    public static function cmpTreeNode($a, $b) {
        if (intval($a["level"]) == intval($b["level"]))
            if (intval($a["order"]) == intval($b["order"]))
                return 0;
            else
                return (intval($a["order"]) < intval($b["order"])) ? -1 : 1;
        else
            return (intval($a["level"]) < intval($b["level"])) ? -1 : 1;
    }

    /*
     * Build tree array from array
     * @param array   $srcArray (must have 3 keys: id, parent_id, order)
     * Ex: $srcArray = array(
     *          <id> => array ("id" => <value>, "parent_id" => <value>, "order" => <value>, ... ),
     *          <id> => array ("id" => <value>, "parent_id" => <value>, "order" => <value>, ... )
     *          )
     */
    public static function buildTreeArray($srcArray) {
        // adding level & path to array for sorting
        $arrayWithPath = array();
        foreach ($srcArray as $node) {
            $level = 1;
            $path = "/" . $node["id"];
            $tmp_parent_id = $node["parent_id"];
            while ($tmp_parent_id != null && $tmp_parent_id != 0) {
                if (array_key_exists($tmp_parent_id, $srcArray)) {
                    $parent_node = $srcArray[$tmp_parent_id];
                    $tmp_parent_id = $parent_node["parent_id"];
                    $level += 1;
                    $path = "/" . $parent_node["id"] . $path;
                } else {
                    $tmp_parent_id = null;
                }
            }
            $node["level"] = $level;
            $node["path"] = $path;
            $arrayWithPath[$path] = $node;
        }

        // sort by level & order
        uasort($arrayWithPath, "LGHelpers::cmpTreeNode");

        // build tree
        $nodes = array();
        $tree = array();
        foreach ($arrayWithPath as &$node) {
            $node["items"] = array();
            $child = $node["id"];
            $parent = $node["parent_id"];

            $nodes[$child] = &$node;
            if (array_key_exists($parent, $nodes)) {
                $nodes[$parent]["items"][] = &$node;
            } else {
                $tree[$node["id"]] = &$node;
            }
        }
        return $tree;
    }

    public static function exposeTreeArray($treeArray, &$outArray, $level = '- ', $skippedId = array()) {
        foreach ($treeArray as $arr) {
            $strIndent = "";
            for ($i = 1; $i < $arr["level"]; $i++) {
                $strIndent = $strIndent . $level;
            }
            $arr["indent"] = $strIndent;

            if (!in_array($arr['id'], $skippedId)) {
                $outArray[] = $arr;
                LGHelpers::exposeTreeArray($arr["items"], $outArray, $level, $skippedId);
            }
        }
    }

    public static function getSummary($gridid = 'DataGrid') {
        return 'Display # ' . Html::dropDownListPageSize($gridid , LGHelpers::getPageSize()) . ' {start}-{end} of {count} result(s).';
    }
    

    public static function thumbnail($thumbnail_file_path, $full_image_file_path, $title = null) {
        $thumbnail_link = '<div class="tafancy" >'.
                            '<a rel="lightbox" href="'. $full_image_file_path .'" title="'. $title .'">'.
                                '<div class="plus"></div>'.
                                '<img src="'. $thumbnail_file_path .'">'.
                            '</a>'.
                          '</div>';
        return $thumbnail_link;
    }

    public static function getWidgetTypePositionListData() {
	$arr = array();
	
	$arr[] = array('id' =>  t('LBL_MENU_POSITION', 'widget'), 
	    'name' => t('LBL_MENU_POSITION', 'widget'));
	$arr[] = array('id' => t('LBL_BANNER_POSITION', 'widget'), 
	    'name' => t('LBL_BANNER_POSITION', 'widget'));
	$arr[] = array('id' => t('LBL_TOP_POSITION', 'widget'), 
	    'name' => t('LBL_TOP_POSITION', 'widget'));
	$arr[] = array('id' => t('LBL_LEFT_POSITION', 'widget'), 
	    'name' => t('LBL_LEFT_POSITION', 'widget'));
	$arr[] = array('id' => t('LBL_RIGHT_POSITION', 'widget'), 
	    'name' => t('LBL_RIGHT_POSITION', 'widget'));
	$arr[] = array('id' => t('LBL_BOTTOM_POSITION', 'widget'), 
	    'name' => t('LBL_BOTTOM_POSITION', 'widget'));
	
	return $arr;
    }
    
    public static function getWidgetTypeListData() {
	$arr = array();
	
	$arr[] = array('id' => LGConstantWidgetType::BANNER, 
	    'name' => LGConstantWidgetType::BANNER);
	$arr[] = array('id' => LGConstantWidgetType::TAB, 
	    'name' => LGConstantWidgetType::TAB);
	$arr[] = array('id' => LGConstantWidgetType::CUSTOM_HTML, 
	    'name' => LGConstantWidgetType::CUSTOM_HTML);
		
	return $arr;
    }
    
    public static function getSearchStatus() {
        $arr = array();
        $arr[] = array('id' => LGConstant::PUBLISH_STATUS, 'name' => t('PUBLISH_STATUS'));
        $arr[] = array('id' => LGConstant::UNPUBLISH_STATUS, 'name' => t('UNPUBLISH_STATUS'));
        return $arr;
    }
    
    public static function getSexListData() {
        $arr = array();
        $arr[] = array('id' => LGConstant::SEX_MALE, 'name' => t('DROP_DOWN_ITEM_MALE', 'profile'));
        $arr[] = array('id' => LGConstant::SEX_FEMALE, 'name' => t('DROP_DOWN_ITEM_FEMALE', 'profile'));        
        return $arr;
    }
    
    public static function getYesNoData() {
        $arr = array();
        $arr[] = array('id' => LGConstant::YES, 'name' => t('DROP_DOWN_ITEM_YES', 'profile'));
        $arr[] = array('id' => LGConstant::NO, 'name' => t('DROP_DOWN_ITEM_NO', 'profile'));        
        return $arr;
    }
    
    public static function getMainHandData() {
    	$arr = array();
    	$arr[] = array('id' => LGConstant::YES, 'name' => t('DROP_DOWN_ITEM_RIGHT_HAND', 'profile'));
    	$arr[] = array('id' => LGConstant::NO, 'name' => t('DROP_DOWN_ITEM_LEFT_HAND', 'profile'));
    	return $arr;
    }
    
    public static function getEyeCapabilityListData() {
        $arr = array();        
        $arr[] = array('id' => 1, 'name' => 1);
        $arr[] = array('id' => 2, 'name' => 2);
        $arr[] = array('id' => 3, 'name' => 3);
        $arr[] = array('id' => 4, 'name' => 4);
        $arr[] = array('id' => 5, 'name' => 5);
        $arr[] = array('id' => 6, 'name' => 6);
        $arr[] = array('id' => 7, 'name' => 7);
        $arr[] = array('id' => 8, 'name' => 8);
        $arr[] = array('id' => 9, 'name' => 9);
        $arr[] = array('id' => 10, 'name' => 10);
        return $arr;
    }
    
    public static function getMonthListData() {
    	$arr = array();
    	$arr[] = array('id' => 1, 'name' => 1);
    	$arr[] = array('id' => 2, 'name' => 2);
    	$arr[] = array('id' => 3, 'name' => 3);
    	$arr[] = array('id' => 4, 'name' => 4);
    	$arr[] = array('id' => 5, 'name' => 5);
    	$arr[] = array('id' => 6, 'name' => 6);
    	$arr[] = array('id' => 7, 'name' => 7);
    	$arr[] = array('id' => 8, 'name' => 8);
    	$arr[] = array('id' => 9, 'name' => 9);
    	$arr[] = array('id' => 10, 'name' => 10);
    	$arr[] = array('id' => 11, 'name' => 11);
    	$arr[] = array('id' => 12, 'name' => 12);
    	return $arr;
    }
    
    public static function getYearListData($minOffset, $maxOffset) {
    	$arr = array();
    	for($i = $minOffset; $i <= $maxOffset; $i++){
    		$arr[] = array('id' => $i, 'name' => $i);
    	}
    	return $arr;
    }
    
    public static function checkVNFieldChanged($arrUpdatedFields, $fieldName){
    	if (in_array($fieldName, $arrUpdatedFields) == TRUE)
    		return ' changed';
    	else 
    		return ' unchanged';
    }
    
    public static function getStatusListData(){
    	$arr = array();
    	$ref = new ReflectionClass('ProfileStatus');
    	$arrStatus = $ref->getConstants();
    	foreach($arrStatus as $i => $status){
    		$arr[]=array('id'=>$status, 'name'=>t("EXPORT_STATUS_" . $status, "profile"));
    	}
    	return $arr;
    }
}
